import api from './index';
interface ResponseData<T> {
  code: number;
  message: string;
  data: T;
}
interface LoginParams {
  username: string;
  password: string;
}

interface RegisterParams {
  username: string;
  phone: string;
  password: string;
}

interface LoginVo {
  userInfo: {
    id: string;
    username: string;
    phone: string;
    password: string;
    avatar: string;
    bio: string;
    courses: number;
    likes: number;
    createdAt: string;
  };
  token: string;
}

/* 用户API */
// 登录
export const loginApi = (data: LoginParams): Promise<ResponseData<LoginVo>> => api.post('/user/login', data);
// 注册
export const registerApi = (data: RegisterParams): Promise<ResponseData<string>> => api.post('/user/register', data);
// 获取用户信息
export const getUserInfoApi = (): Promise<ResponseData<LoginVo['userInfo']>> => api.get('/user/info');
// 文件上传
export const uploadFileApi = (data: FormData): Promise<ResponseData<string>> => api.post('/upload/img', data);
// 更新用户信息
export const updateUserInfoApi = (data: LoginVo['userInfo']): Promise<ResponseData<string>> => api.post('/user/update', data);
// 修改密码
export const updatePasswordApi = (data: { oldPassword: string; newPassword: string }): Promise<ResponseData<string>> => api.post('/user/updatePassword', data);
// 换绑手机号
export const updatePhoneApi = (data: { phone: string; }): Promise<ResponseData<string>> => api.post('/user/updatePhone', data);

/* 课程API */

export interface CourseList {
  id: string;
  name: string;
  description: string;
  type: string;
  category: string;
  content: string;
  image: string;
  video_url: string;
  created_at: string;
  updated_at: string;
}

/* 课程列表 */
export const getCourseListApi = (): Promise<ResponseData<{
  festival: CourseList[]
  solarTerm: CourseList[]
  zodiac: CourseList[]
  chineseZodiac: CourseList[]
}>> => api.get('/course/list');


/* 课程分类API */

export interface CourseCategory {
  key: string;
  label: string;
  children: {
    id: string;
    label: string;
    parent_id: string;
  }[]
}

// 获取课程分类列表选项
export const getCourseCategoryApi = (type: 'festival' | 'solarTerm' | 'zodiac' | 'chineseZodiac'): Promise<ResponseData<CourseCategory[]>> => api.get(`/course/category/${type}`);
// 根据查询参数查询课程列表
export const getCourseListByQueryApi = (params: {
  type: 'festival' | 'solarTerm' | 'zodiac' | 'chineseZodiac';
  classify: string;
  page: number;
  pageSize: number;
}): Promise<ResponseData<{
  data: CourseList[];
  total: number;
}>> => api.post(`/course/courseList/${params.type}?classify=${params.classify}`, {
  page: params.page,
  pageSize: params.pageSize,
});

export interface Comment {
  id: string;
  user_id: string; // 用户id
  course_id: string; // 课程id
  content: string; // 评论内容
  parent_id: string | null; // 父评论id
  root_id: string | null; // 根评论id
  created_at: Date | null; // 创建时间
  updated_at: Date | null; // 更新时间
  user_info: {
    id: string;
    username: string;
    avatar: string;
  }
}

// 获取课程的评论
export const getCourseCommentApi = (
  courseId: string
): Promise<ResponseData<Comment[]>> => api.get(`/course/comment/${courseId}`);

// 发表评论
export const addCourseCommentApi = (data: { content: string, course_id: string, parent_id: string | null, root_id: string | null }): Promise<ResponseData<string>> => api.post('/course/comment/publish', data);

// 删除评论
export const deleteCourseCommentApi = (id: string): Promise<ResponseData<string>> => api.delete(`/course/comment/${id}`);

// 获取课程详情
export const getCourseDetailApi = (courseId: string): Promise<ResponseData<CourseList>> => api.get(`/course/courseDetail/${courseId}`);

// 将课程加入我的课程或者从我的课程中移除
export const joinCourseApi = (data: { course_id: string }): Promise<ResponseData<string>> => api.post(`/course/addMyCourse`, data);

// 获取我的课程列表
export const getMyCourseListApi = (): Promise<ResponseData<CourseList[]>> => api.get(`/course/myCourseList`);

// 判断课程是否加入我的课程
export const isJoinCourseApi = (courseId: string): Promise<ResponseData<boolean>> => api.get(`/course/isJoinCourse/${courseId}`);

// 获取我的评论数
export const getMyCommentCountApi = (): Promise<ResponseData<number>> => api.get(`/course/myCommentCount`);